package com.ex03.ylw.connector.http;

import org.apache.catalina.util.StringManager;

import java.io.OutputStream;
import java.net.Socket;

/**
 * 1.创建HttpRequest
 * 2.创建HttpResponse
 * 3.解析HTTP请求的第一行和头信息，填充HttpRequest对象
 * 4.将HttpRequest对象和HttpResponse对象传递给ServletProcessor和StaticResourceProcessor
 * Created by yangliwei on 2017/7/25.
 */
public class HttpProcessor {
    public HttpProcessor(HttpConnector httpConnector) {
        this.connector = httpConnector;
    }

    private HttpConnector connector = null;
    private HttpRequest request;
    private HttpRequestLine requestLine = new HttpRequestLine();
    private HttpResponse response;

    protected String method = null;
    protected String queryString = null;
    protected StringManager sm =
            StringManager.getManager("com.ex03.ylw.connector.http");

    public void process(Socket socket) {
        SocketInputStream input = null;
        OutputStream output = null;
//        try {
//            input = new SocketInputStream(socket.getInputStream(), 2048);
//            output = socket.getOutputStream();
//
//            // create HttpRequest object and parse
//            request = new HttpRequest(input);
//
//            // create HttpResponse object
//            response = new HttpResponse(output);
//            response.setRequest(request);
//
//            response.setHeader("Server", "Pyrmont Servlet Container");
//
//            parseRequest(input, output);
//            parseHeaders(input);
//
//            //check if this is a request for a servlet or a static resource
//            //a request for a servlet begins with "/servlet/"
//            if (request.getRequestURI().startsWith("/servlet/")) {
//                ServletProcessor processor = new ServletProcessor();
//                processor.process(request, response);
//            }
//            else {
//                StaticResourceProcessor processor = new StaticResourceProcessor();
//                processor.process(request, response);
//            }
//
//            // Close the socket
//            socket.close();
//            // no shutdown for this application
//        }
//        catch (Exception e) {
//            e.printStackTrace();
//        }
    }
}


